package com.newcapec.modules.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.newcapec.common.utils.PageUtils;
import com.newcapec.common.utils.Query;
import com.newcapec.modules.health.dao.CustomerDao;
import com.newcapec.modules.health.entity.CustomerEntity;
import com.newcapec.modules.health.service.CustomerService;
import com.newcapec.modules.oss.cloud.OSSFactory;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("customerService")
public class CustomerServiceImpl extends ServiceImpl<CustomerDao, CustomerEntity> implements CustomerService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String name = (String)params.get("name");

        IPage<CustomerEntity> page = this.page(
                new Query<CustomerEntity>().getPage(params),
                new QueryWrapper<CustomerEntity>()
                    .like(StringUtils.isNotBlank(name),"name", name)
        );
        String ossDomain = OSSFactory.getOssDomain();
        page.getRecords().stream().forEach(item -> item.setLogo(ossDomain + item.getLogo()));

        return new PageUtils(page);
    }

    @Override
    public CustomerEntity get(Long id) {
        CustomerEntity customer = this.getById(id);
        if(customer!=null && StringUtils.isNotBlank(customer.getLogo()) && !customer.getLogo().startsWith("http")){
            String ossDomain = OSSFactory.getOssDomain();
            customer.setLogo(ossDomain + customer.getLogo());
        }
        return customer;
    }

}